package com.china08.yxyapi.api.manage.edu.platform.lesson;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.cloud.YxyLessonsCloudLesson;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.repository.db.lesson.YxyLessonsCloudLessonRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class EduLessonUpdController extends YxyApiBaseController {

    @Autowired
    YxyLessonsCloudLessonRepository lessonRepository;

    @ApiOperation("")
    @RequestMapping(value = "/api_yw/manage/edu/platform/lesson/upd", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String lessonId = reqModel.lessonId;
        String rejectReason = reqModel.rejectReason;
        Integer status = reqModel.status;
        Integer isJingpinLesson = reqModel.isJingpinLesson;

        YxyLessonsCloudLesson lesson = lessonRepository.findById(lessonId);
        if (null != lesson) {
            lesson.setStatus(status);
            lesson.setIsJingpinLesson(isJingpinLesson);
            lesson.setRejectReason(rejectReason);
            if (status == 2) {
                // 上架
                lesson.setIsVisible(1);
                lesson.setUpDate(new Date());
            }
            lesson.setAuditDate(new Date());
            lesson.setUpdateDate(new Date());
            lessonRepository.saveAndFlush(lesson);
        }
    }

    public static class ReqModel extends YxyApiReqModel {
        public String lessonId;
        public Integer status;
        public Integer isJingpinLesson;
        public String rejectReason;
    }
}
